package cn.brilliantfuture.commentry.listener;

import java.text.ParseException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import cn.brilliantfuture.commentry.sms.SMSDelaySendJob;
import cn.brilliantfuture.commentry.warn.TakeoutWarnJob;

/**
 * 
 * Title: Communication Entry
 * 
 * Author:  LiTian  
 *
 * Date: 2012-2-17
 * 
 * Description:
 * 
 */
public class CommentryServletContextListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent arg0) {
	}

	public void contextInitialized(ServletContextEvent arg0) {
		
		try {
			SchedulerFactory schedulerFactory = new StdSchedulerFactory();
			
			Scheduler scheduler = schedulerFactory.getScheduler();
		
			//JobDetail idpJobDetail = new JobDetail("taskJob", "IDPGroup", InvokeDeliveryPlanJob.class);
			//JobDetail rceJobDetail = new JobDetail("taskJob", "RCEGroup", RechargeCardExpireValidJob.class);
			JobDetail sdsJobDetail = new JobDetail("taskJob", "SDSGroup", SMSDelaySendJob.class);
			JobDetail twJobDetail = new JobDetail("taskJob", "TWGroup", TakeoutWarnJob.class);
		
			//CronTrigger idpcronTrigger = new CronTrigger("IDPCronTrigger", "IDPGroup");
			//CronTrigger rceCronTrigger = new CronTrigger("RCECronTrigger", "RCEGroup");
			CronTrigger sdsCronTrigger = new CronTrigger("SDSCronTrigger", "SDSGroup");
			CronTrigger twCronTrigger = new CronTrigger("TWCronTrigger", "TWGroup");

			//CronExpression idpCronExpression = new CronExpression("0 0 23 * * ?");
			//idpcronTrigger.setCronExpression(idpCronExpression);
			
			//CronExpression rceCronExpression = new CronExpression("0 10 0 * * ?");
			//rceCronTrigger.setCronExpression(rceCronExpression);
			
			CronExpression sdsCronExpression = new CronExpression("0 10 7 * * ?");
			CronExpression twCronExpression = new CronExpression("0 0 7,16 * * ?");
			sdsCronTrigger.setCronExpression(sdsCronExpression);
			twCronTrigger.setCronExpression(twCronExpression);

			//scheduler.scheduleJob(idpJobDetail, idpcronTrigger);
			//scheduler.scheduleJob(rceJobDetail, rceCronTrigger);
			scheduler.scheduleJob(sdsJobDetail, sdsCronTrigger);
			scheduler.scheduleJob(twJobDetail, twCronTrigger);
			
			scheduler.start();
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}

		//当时发送提示短信 
		/*Timer timer = new Timer();
		timer.schedule(new TakeoutWarnTask(), 0, 15 * 60 * 1000);*/
	}

}
